package com.kahluastudios.mealfinder.desktop.server;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;





import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;

import com.google.gwt.user.server.Base64Utils;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.kahluastudios.mealfinder.desktop.client.services.RecommendationsService;
import com.kahluastudios.mealfinder.desktop.shared.RecommendationVO;

public class RecommendationsServiceImpl 	extends RemoteServiceServlet 
																	implements RecommendationsService {
	private static final long serialVersionUID = 3052022231972149753L;
	
	
	private String placeholderImage;
	
	public RecommendationsServiceImpl() 
	{ this.placeholderImage = getPlaceholderImage(); }
	
	public List<RecommendationVO> getRecent() {
		Collection<RecommendationVO> recommendations = 
				SpoofDataStore.getInstance().getRecommendations().values();		
		return processForDisplay(recommendations); 
	}

	protected List<RecommendationVO> processForDisplay(
			Collection<RecommendationVO> recommendations) {
		List<RecommendationVO> result =  new ArrayList<RecommendationVO>();		
		String restaurantImage;
		for (RecommendationVO recommendationVO : recommendations) {
			restaurantImage = 
					recommendationVO.getRestaurantInfo().getEncodedRestaurantImage();
			if (!StringUtils.isEmpty(restaurantImage)) {
				recommendationVO.getRestaurantInfo().setEncodedRestaurantImage(
																this.placeholderImage);
			}
		}
		return result;
	}
	
	private String getPlaceholderImage() {		
		String result;
		InputStream resourceStream = 
				getClass().getResourceAsStream("images/plate.jpg");
		try {
			byte[] imageBytes = IOUtils.toByteArray(resourceStream);
			result = Base64Utils.toBase64(imageBytes);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}		
		return result;
	}//end method def
}//end class def